package com.stations.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.stations.common.constant.ControllerConstant;
import com.stations.common.constant.SendTransmitterPostPathConstants;
import com.stations.common.utils.StringUtils;
import com.stations.common.web.domain.response.Result;
import com.stations.domain.*;
import com.stations.mapper.TTransmitterMapper;
import com.stations.service.TransmitterSendService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.Exception;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Describe: 发射机查询设置管理服务实现类
 * Author: Ljy
 * CreateTime: 2019/10/23
 */
@Service("TransmitterSendService")
public class TransmitterSendServiceImpl extends ServiceImpl<TTransmitterMapper, TTransmitter> implements TransmitterSendService {

    @Resource
    TTransmitterMapper transmitterMapper;
    @Resource(name = "TerminalRedisTemplate")
    RedisTemplate<String,Object> redisTemplate;
    @Resource
    private TTransmitterMapper tTransmitterMapper;

    @Override
    public Result communication(TTransmitter transmitter) {
        JSONObject jsonObject = new JSONObject();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        jsonObject.put("transName", transmitter.getTransName());
        jsonObject.put("transIP", transmitter.getTransIp1());
        jsonObject.put("stationIP", transmitter.getStationIP());
        jsonObject.put("stationPort", transmitter.getStationPort());
        jsonObject.put("transComProtocol", transmitter.getTransComProtocol());
        jsonObject.put("statusPeriod", transmitter.getStatusPeriod());
        jsonObject.put("oneTaskPlayTime", transmitter.getOneTaskPlayTime());
        jsonObject.put("sendTime", sdf.format(new Date()));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String value = HttpUtil.post(http + SendTransmitterPostPathConstants.communication, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public Result playTask(TTransmitterTask transmitterTask) {
        JSONObject jsonObject = new JSONObject();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("stationCode", transmitterTask.getStationCode());
        jsonObject.put("transCode", transmitterTask.getTransCode());
        jsonObject.put("taskId", transmitterTask.getTaskId());
        jsonObject.put("sendDateTime", sdf.format(new Date()));
        jsonObject.put("operType", transmitterTask.getOperType());
        jsonObject.put("channelFreq", transmitterTask.getChannelFreq());
        jsonObject.put("channelRfChState", transmitterTask.getChannelRfChState());
        jsonObject.put("channelTimeSlot", transmitterTask.getChannelTimeSlot());
        jsonObject.put("channelEqualization", transmitterTask.getChannelEqualization());
        jsonObject.put("channelRfChMod", transmitterTask.getChannelRfChMod());
        jsonObject.put("channelRfChBand", transmitterTask.getChannelRfChBand());
        jsonObject.put("channelRfChASource", transmitterTask.getChannelRfChASource());
        jsonObject.put("channelRfChAUrl", transmitterTask.getChannelRfChAUrl());
        jsonObject.put("sendTime", sdf.format(new Date()));
        String http = "http://" + transmitterTask.getTransIp() + ":" + transmitterTask.getTransHttpPort();
        String https = "https://" + transmitterTask.getTransIp() + ":" + transmitterTask.getTransHttpsPort();
        String value = HttpUtil.post(http + SendTransmitterPostPathConstants.playTask, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public Result setStatus(SRunStatus sRunStatus,TTransmitter transmitter) {
        JSONObject jsonObject = new JSONObject();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("stationCode", sRunStatus.getStationCode());
        jsonObject.put("transCode", sRunStatus.getTransCode());
        jsonObject.put("risingPower", sRunStatus.getRisingPower());
        jsonObject.put("dropPower", sRunStatus.getDropPower());
        jsonObject.put("transReset", sRunStatus.getTransReset());
        jsonObject.put("audioVolumeUp", sRunStatus.getAudioVolumeUp());
        jsonObject.put("audioVolumedowen", sRunStatus.getAudioVolumedowen());
        jsonObject.put("channelSet",sRunStatus.getTransReset());
        jsonObject.put("sendTime", sdf.format(new Date()));
        sRunStatus.setSendTime(new Date());
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String value =  HttpUtil.post(http + SendTransmitterPostPathConstants.status, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public Result setFunction(TTransmitterThreshold tTransmitterThreshold,TTransmitter tnowTTransmitter) {
        tTransmitterThreshold.setTransIp(tnowTTransmitter.getTransIp1());
        tTransmitterThreshold.setTransHttpPort(tnowTTransmitter.getTransHttpPort());
        tTransmitterThreshold.setTransHttpsPort(tnowTTransmitter.getTransHttpsPort());
        tTransmitterThreshold.setSendTime(new Date());
        String http = "http://" + tnowTTransmitter.getTransIp1() + ":" + tnowTTransmitter.getTransHttpPort();
        String https = "https://" + tTransmitterThreshold.getTransIp() + ":" + tTransmitterThreshold.getTransHttpsPort();
        String value = HttpUtil.post(http + SendTransmitterPostPathConstants.function, StringUtils.ObjectToString(tTransmitterThreshold));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public Result clock(TTransmitter transmitter) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("newTime", sdf.format(new Date()));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String https = "https://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpsPort();
        String value = HttpUtil.post(http + SendTransmitterPostPathConstants.clock, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public Result readClock(TTransmitter transmitter) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("sendTime", sdf.format(new Date()));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String https = "https://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpsPort();
        String value =  HttpUtil.post(http + SendTransmitterPostPathConstants.readClock, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public Result runStatus(TTransmitter transmitter) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("newTime", sdf.format(new Date()));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String https = "https://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpsPort();
        String value =  HttpUtil.post(http + SendTransmitterPostPathConstants.queryStatus, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public TTransmitterException[] queryException(TTransmitter transmitter,Date sendTime) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("sendTime", sdf.format(sendTime));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String https = "https://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpsPort();
        String value =  HttpUtil.post(http +SendTransmitterPostPathConstants.queryException, StringUtils.ObjectToString(jsonObject));
//        {"code":"0","data":{"exceptionList":[]},"msg":""}
        JSONObject jsonresult = JSON.parseObject(value);
        JSONObject data = JSON.parseObject(jsonresult.getString("data"));
        ObjectMapper objectMapper = new ObjectMapper();
        TTransmitterException[] tTransmitterException = new TTransmitterException[0];
        try {
            tTransmitterException = objectMapper.readValue(data.getString("exceptionList"), TTransmitterException[].class);
        } catch (JsonProcessingException e) {
            log.error("获取发射机告警日志错误");
        }
        return tTransmitterException;
    }

    @Override
    public  TTransmitterLog[] queryLog(TTransmitter transmitter, String startTime, String endTime) {
        JSONObject jsonObject = new JSONObject();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        jsonObject.put("startTime", startTime);
        jsonObject.put("endTime", endTime);
        jsonObject.put("sendTime", sdf.format(new Date()));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String https = "https://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpsPort();
        String value =  HttpUtil.post(http + SendTransmitterPostPathConstants.queryLog, StringUtils.ObjectToString(jsonObject));
        JSONObject jsonresult = JSON.parseObject(value);
        JSONObject data = JSON.parseObject(jsonresult.getString("data"));
        ObjectMapper objectMapper = new ObjectMapper();
        TTransmitterLog[] tTransmitterLog = new TTransmitterLog[0];
        try {
             tTransmitterLog = objectMapper.readValue(data.getString("logList"), TTransmitterLog[].class);
        } catch (JsonProcessingException e) {
            log.error("获取发射机日志错误");
        }
        return tTransmitterLog;
    }

    @Override
    public Result queryStatus(TTransmitter transmitter) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("newTime", sdf.format(new Date()));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String https = "https://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpsPort();
        String value =  HttpUtil.post(http + SendTransmitterPostPathConstants.queryStatus, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public Result queryFunction(TTransmitter transmitter) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("newTime", sdf.format(new Date()));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String https = "https://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpsPort();
        String value = HttpUtil.post(http + SendTransmitterPostPathConstants.queryFunction, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }

    @Override
    public Result queryCommunication(TTransmitter transmitter) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stationCode", transmitter.getStationCode());
        jsonObject.put("transCode", transmitter.getTransCode());
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        jsonObject.put("newTime", sdf.format(new Date()));
        String http = "http://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpPort();
        String https = "https://" + transmitter.getTransIp1() + ":" + transmitter.getTransHttpsPort();
        String value = HttpUtil.post(http + SendTransmitterPostPathConstants.queryCommunication, StringUtils.ObjectToString(jsonObject));
        return JSON.parseObject(value,Result.class);
    }
    /**
     * Describe: 获取发射机
     * Param ModelAndView
     * Return 获取发射机
     */
    @Override
    public TTransmitter getTTransmitter(String stationCode,String transCode) {
        QueryWrapper<TTransmitter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_code", stationCode);
        queryWrapper.eq("trans_code", transCode);
        return tTransmitterMapper.selectOne(queryWrapper);
    }
}
